<template>
  <div>
    <notice-card v-for="(val,index) in message" :key="val.messageId" :info="val"></notice-card>
    <empty-card v-if="message.length===0" height="556px"></empty-card>
    <pagination ref="pagination" style="margin: 0 auto;padding:20px 0;" :pages="pages" v-on:listenPageChange="changePage"></pagination>

  </div>
</template>


<script>
  import NoticeCard from '../../../components/pc/noticeCard';
  import config from '../../../action/config';
  import Pagination from "../../../components/pc/pagination";
  import EmptyCard from "../../../components/pc/emptyCard";
  import {globalBus} from "../../../components/common/globalBus";

  export default {
    name:'comment',
    data(){
      return{
        message:[],
        pages:1,
        currentPage: 1
      }
    },
    components:{
      NoticeCard,Pagination,EmptyCard
    },
    created() {
      globalBus.$on(config.event.listenMessageClearAll,(()=>{
        this.RefreshCurrentPage()
      }))
    },
    sockets: {
      push_event(data) {
        this.RefreshCurrentPage()
      }
    },
    methods:{
      RefreshCurrentPage(){
        if(process.browser){
          clearTimeout(this.refreshTime)
          let that = this;
          this.refreshTime = setTimeout(()=>{
            that.message=[];
            that.$refs.pagination.RefreshCurrentPage()
          })
        }
      },

      dataGetMessages(currentPage) {
        this.$axios.$get(config.api.get.Message.list, {
          params: {
            page: currentPage,
            size: 10,
            type:'COMMENT'
          }
        }).then((response) => {
          this.message = response.data.dataList;
          this.pages = response.data.totalPage;

        });

      },
      dataPutReadAll() {
        this.$axios.$put(config.api.put.Message.readAll).then((response) => {
          if(response.code===0){
            this.$message({
              type: 'success',
              message: '已全部标记已读'
            });
            this.message = [];
            this.restartPagination();
          } else {
            this.$message({
              type: 'warning',
              message: '没有未读信息'
            });
          }
        })
      },
      changePage(currentPage){
        //返回页数 请求新的数据
        this.dataGetMessages(currentPage)
        this.currentPage = currentPage
      }
    },
  }
</script>
